﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;

namespace QuanLyKho.DKChung
{
    public partial class DkChung_KhoNoiBo_ThemMoi : Form
    {
        public static string MaID = "0";

        public DkChung_KhoNoiBo_ThemMoi()
        {
            InitializeComponent();
            if (MaID != "0")
                LoadFormUpdate();
            else
                LoadFormInsert();
        }

        private void LoadFormInsert()
        {
            txtMaKho.Text = txtTenKho.Text = txtMoTa.Text = string.Empty;
            MaID = "0";
        }

        private void LoadFormUpdate()
        {
            DataTable dt = DataAll.SelectAll("Kho_KhoHang", "*", "Where IDKhoHang= " + MaID);
            txtMaKho.Text = dt.Rows[0]["MaKho"].ToString();
            txtTenKho.Text = dt.Rows[0]["TenKho"].ToString();
            txtMoTa.Text = dt.Rows[0]["MoTa"].ToString();
        }

        private void bntLuuLai_Click(object sender, EventArgs e)
        {
            if (!ValidateForm())
                return;
            if (MaID != "0")
            {
                string message = DataAll.Update("Kho_KhoHang", "MaKho=N'" + txtMaKho.Text + "', TenKho=N'" + txtTenKho.Text
                   + "',MoTa=N'" + txtMoTa.Text + "' WHERE IDKhoHang=" + MaID);
                MessageBox.Show(message.Equals("success") ? "Sửa kho thành công" : message, "Message",
                                MessageBoxButtons.OK,
                                message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                Close();
            }
            else
            {
                DataTable dt = DataAll.SelectCustomer("INSERT INTO [Kho_KhoHang] VALUES(N'" + txtMaKho.Text + "',N'" + txtTenKho.Text + "',N'" + txtMoTa.Text + "')SELECT MAX(IDKhoHang) FROM Kho_KhoHang");
                DataAll.SelectCustomer("INSERT INTO Kho_KhoHang_VatTu (IDVatTu,IDKhoHang,SoLuong) (SELECT IDVatTu,(SELECT IDKhoHang FROM Kho_KhoHang where IDKhoHang=" + dt.Rows[0][0] + "),0 FROM Kho_VatTu) SELECT MAX(IDKhoHang) FROM Kho_KhoHang");

                MessageBox.Show(dt.Rows.Count > 0 ? "Thêm kho thành công" : "Xảy ra lỗi, không thể đăng ký kho.", "Message",
                                MessageBoxButtons.OK,
                                dt.Rows.Count > 0 ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                if (dt.Rows.Count > 0)
                {
                    LoadFormInsert();
                }
            }
        }

        private bool ValidateForm()
        {
            if (string.IsNullOrEmpty(txtMaKho.Text))
            {
                MessageBox.Show("Vui lòng nhập mã kho !", "Lỗi nhập dữ liệu", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            if (string.IsNullOrEmpty(txtTenKho.Text))
            {
                MessageBox.Show("Vui lòng nhập tên kho !", "Lỗi nhập dữ liệu", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            return true;
        }

        private void bntThoat_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
